package com.yld.demo2.controller;


import com.alibaba.fastjson.JSONObject;
import com.yld.demo2.entity.vo.OrderListVo;
import com.yld.demo2.service.OrderListService;
import com.yld.demo2.util.DataUtil;
import com.yld.demo2.util.tooltips.ResBean;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/kafka")
public class OrderListKafkaRedisController {

    //关联数据
    List<String> userIdList = new ArrayList<>();

    @Autowired
    OrderListService orderListService;

    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    KafkaTemplate kafkaTemplate;


    @PutMapping("/insertRedisAll")
    @ApiOperation("添加方法 添加所有")
    public ResBean insertAll(@RequestBody OrderListVo orderList){
        if (orderList.getOrdernum() == 0 | orderList.getAmout() == 0){
            return ResBean.numberOrMoneyNotZero();
        }
        //获取当前 年月日
        OrderListVo orderListVo = DataUtil.nowDate(orderList);
        System.out.println("接下来执行insert-kafka===========================");
        kafkaTemplate.send("insert-kafka", JSONObject.toJSONString(orderListVo));
        //数据库添加
        //添加到redis
        redisTemplate.opsForList().rightPush("userId="+orderList.getUserid(),orderListVo);
        return ResBean.success("新增成功！！！");
    }

    @ApiOperation("根据订单号order_no 修改")
    @PutMapping("/updateByOrderNoRedis")
    public ResBean updateByOrderNo(@RequestBody OrderListVo orderListVo){
        // 金额 和 交易数量 不能为0
        if (orderListVo.getOrdernum() == 0 | orderListVo.getAmout() == 0){
            return ResBean.numberOrMoneyNotZero();
        }
        System.out.println("接下来执行update-kafka===========================");
        kafkaTemplate.send("update-kafka",JSONObject.toJSONString(orderListVo));
 /*       //redis缓存的替换
        redisTemplate.opsForList().getOperations().delete("userId="+orderListVo.getUserid());

        //数据库的修改操作
        Boolean aBoolean = orderListService.updateByOrderNo(orderListVo);*/


        return ResBean.success("更新成功!!!");
    }





    @ApiOperation("根据订单号order_no 删除")
    @DeleteMapping("/deleteRedisByOrderNo/{orderno}")
    public ResBean deleteByOrderNo(@PathVariable Integer orderno){
        kafkaTemplate.send("delete-kafka",orderno.toString());
        return ResBean.success("删除成功！！！");
    }


























 /*   @GetMapping("/selectRedisAllByUserId/{user_id}")
    @ApiOperation("根据用户编号user_id查询")
    public ResBean selectByUserId(@PathVariable("user_id") Integer userid){

        //数据校验
        if(userid==null||" ".equals(userid)){
            return ResBean.FAIL_DML();
        }
        //先查询redis缓存
        else  if(redisTemplate.opsForList().getOperations().hasKey("userId="+userid)){

            System.out.println("在redis中查询！！！");
            //true 则返回结果
            List range = redisTemplate.opsForList().range("userId=" + userid, 0, -1);
            for (Object o : range) {
                System.out.println(o.toString());
            }
            //验证是否集合为空
            if(range.isEmpty()){
                return ResBean.noData();
            }else{
                return ResBean.success(range);
            }

        }
        //查询数据库
        else {
            System.out.println("在数据库中查询！！！");
            //通过userid得到全部订单
            List<OrderListVo> orderListVos = orderListService.selectAllByUserId(userid);
            //全部订单写到redis
            if(orderListVos==null){
                return  ResBean.noData();
            }else{
                redisTemplate.opsForList().leftPushAll("userId="+userid,orderListVos);
                return ResBean.success(orderListVos);
            }

        }


    }
    */
/*

    @GetMapping("/selectRedisAll")
    @ApiOperation("查询全部")
    public ResBean selectAll(){
        //       OrderListVo OLvo;
//        try {
//            Date sDate = DateFormatConversion.strToDate(startDate);
//            Date eDate = DateFormatConversion.strToDate(endDate);
//        } catch (ParseException e) {
//            return ResBean.formatDoesNotMatch();
//        }
        List<OrderListVo> orderListVos = orderListService.selectAll();

        System.out.println(orderListVos);

        return ResBean.success(orderListVos);

//        //根据 关联数据 查询 redis
//        if ((userIdList = redisTemplate2.opsForList().range(userid,0,userIdList.size())) != null){
//            return ResBean.success(redisTemplate1.opsForValue().get(userIdList));
//        }
//        //直接查询 数据库
//        if ((OLvo = orderListService.selectByUserId(userid)) != null){
//            redisTemplate2.opsForList().leftPushAll(userid, OLvo.getOrderno());
//        }

    }
*/





}
